package dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

// 通过这个类, 封装针对 blog 表的增删改查操作
// Dao => Data Access Object
public class BlogDao {
    // 1. 新增一个博客
    //    调用 insert 的时候, 需要先构造一个 Blog 对象.
    //    作为 参数 传递给 insert. 再由 insert 内部完成数据库的插入操作.
    public void insert(Blog blog)  {
        Connection connection=null;
        PreparedStatement statement=null;
        try {
            //构建链接
            connection = DBUtil.getConnection();
            //构造查询语句
            String sql = "insert into blog values(null,?,?,?,now())";//获取当前时间，是SQL中提供的
            statement = connection.prepareStatement(sql);
            statement.setString(1, blog.getTitle());
            statement.setString(2, blog.getContent());
            statement.setInt(3, blog.getUserId());
            //执行
            statement.executeUpdate();
        }catch (SQLException e)
        {
            e.printStackTrace();
        }finally {
            //关闭
            DBUtil.close(connection,statement,null);
        }
    }
    //查新所有的bolg
    public List<Blog> getBlogs()
    {
        Connection connection=null;
        PreparedStatement statement=null;
        ResultSet resultSet=null;
        List<Blog> blogs= new ArrayList<>();
        try {
            //创建连接
            connection=DBUtil.getConnection();
            //建立sql语句
            String sql="select* from blog order by postTime desc";
            statement=connection.prepareStatement(sql);
            //执行
            resultSet=statement.executeQuery();
            //遍历结果集合
            while (resultSet.next())
            {
                Blog blog=new Blog();
                blog.setBlogId(resultSet.getInt("blogId"));
                blog.setTitle(resultSet.getString("title"));
                blog.setContent(resultSet.getString("content"));
                blog.setUserId(resultSet.getInt("userId"));
                blog.setPostTime(resultSet.getTimestamp("postTime"));
                blogs.add(blog);
            }
            return blogs;
        }catch (SQLException e)
        {
            e.printStackTrace();
        }
        finally {
            DBUtil.close(connection,statement,resultSet);

        }
        //如果出现异常，会返回一个空的blogs
        return blogs;
    }
    //指定blogId查询博客
    public Blog getBlog(int blogId)
    {
        Connection connection=null;
        PreparedStatement statement=null;
        ResultSet resultSet=null;
        Blog blog=new Blog();
        try {
            //建立连接
            connection=DBUtil.getConnection();
            //构造sql语句
            String sql="select * from blog where blogId=?";
            statement=connection.prepareStatement(sql);
            statement.setInt(1,blogId);
            //执行
            resultSet=statement.executeQuery();
            //遍历
            if(resultSet.next())
            {

                blog.setBlogId(resultSet.getInt("blogId"));
                blog.setTitle(resultSet.getString("title"));
                blog.setContent(resultSet.getString("content"));
                blog.setUserId(resultSet.getInt("userId"));
                blog.setPostTime(resultSet.getTimestamp("postTime"));
            }
            return blog;

        }catch (SQLException e)
        {
            e.printStackTrace();
        }finally {
            DBUtil.close(connection, statement,resultSet);
        }
        return  blog;
    }
    //指定删除
    public void delete(int blogId)
    {
        Connection connection=null;
        PreparedStatement statement=null;
        try {
            //建立连接
            connection=DBUtil.getConnection();
            //创建sql语句
            String sql="delete from blog where blogId=?";
            statement=connection.prepareStatement(sql);
            statement.setInt(1,blogId);
            //执行
            statement.executeUpdate();

        }catch (SQLException e)
        {
            e.printStackTrace();
        }finally {
            //关闭
            DBUtil.close(connection,statement,null);
        }
    }
}